Find HCF of Two Numbers

Theory:

The highest common factor (HCF) of two numbers is the largest positive integer that divides both numbers without leaving a remainder.

Python Code:

def find_hcf(x, y):
    while y:
        x, y = y, x % y
    return x

def find_and_display_hcf():
    x = int(input("Enter the first number: "))
    y = int(input("Enter the second number: "))
    hcf = find_hcf(x, y)
    print("HCF of", x, "and", y, "is:", hcf)

find_and_display_hcf()

Example Output 1:

Enter the first number: 12

Enter the second number: 18

HCF of 12 and 18 is: 6

Example Output 2:

Enter the first number: 24

Enter the second number: 36

HCF of 24 and 36 is: 12

Code Explanation:

The function find_hcf(x, y) calculates the highest common factor (HCF) of two numbers using the Euclidean algorithm.

The function find_and_display_hcf() takes input for two numbers, calculates their HCF using the first function, and displays the result.